
#ifndef __KNSOCK_H__
#define __KNSOCK_H__

#include "knposh.h"

/*
#if defined _WIN32 || defined WIN32 || defined __NT__ || defined __WIN32__
#  define POSH_OS_WIN32 1
#else
#endif
*/

#ifdef POSH_OS_WIN32
#include <winsock2.h>

#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <netinet/in.h>
#include <netdb.h>
#endif



#if defined POSH_OS_WIN32
#define CLOSESOCKET(s) closesocket(s)
#define SOCKERR() WSAGetLastError()
#else
typedef int SOCKET;
#define INVALID_HANDLE -1
#define CLOSESOCKET(s) close(s)
#define SOCKERR() errno
#define WSAWOULDBLOCK EWOULDBLOCK
#endif


#ifdef POSH_OS_WIN32
class WSA
{
private:
	WSA(): ok(false)
	{
		WORD wVersionRequested;
		WSADATA wsaData;
		int err;
		wVersionRequested = MAKEWORD( 2, 2 );
		err = WSAStartup( wVersionRequested, &wsaData );
		if ( err != 0 ) {
			perror("No usable WinSock DLL");
			exit(1);
		}
		if ( LOBYTE( wsaData.wVersion ) != 2 ||
		     HIBYTE( wsaData.wVersion ) != 2 ) {
			perror("No usable WinSock DLL");
			WSACleanup();
			exit(1);
		}
		ok = true;
	}
	
	~WSA()
	{
		if(ok)
			WSACleanup();
	}
private:
	WSA(const WSA&);
	bool ok;
	static WSA instance;
};

WSA WSA::instance;
#endif


#endif

